//
//  RegisterViewController.swift
//  rxCoreLogic
//
//  Created by yq on 2021/2/5.
//

import UIKit
import RxSwift
import RxCocoa

class RegisterViewController: YViewController {

	@IBOutlet weak var baseView: UIView!
	@IBOutlet weak var phoneTextField: UITextField!
	@IBOutlet weak var passwordTextField: UITextField!
	@IBOutlet weak var passwordTwiceTextField: UITextField!
	@IBOutlet weak var passwordTipsLabel: UILabel!

	// 有效检测按钮
	@IBOutlet weak var passwordCountValidButton: UIButton!
	@IBOutlet weak var passwordFormatValidButton: UIButton!

	// 分割线
	@IBOutlet weak var phoneNumberLine: UIView!
	@IBOutlet weak var passwordLine: UIView!
	@IBOutlet weak var passwordTwiceLine: UIView!
	
	@IBOutlet weak var bottomButton: UIButton!
	
	
	override func viewDidLoad() {
        super.viewDidLoad()

		setupUI()
		binding()
    }
}

//MARK: - ✨== setupUI, binding ==✨
extension RegisterViewController {
	
	func setupUI() {
		view.backgroundColor = UIColor(red:0.95, green:0.95, blue:0.95, alpha:1.00)
		baseView.layer.cornerRadius = 6
		baseView.clipsToBounds = true
	}

	func binding(){
		
		let phone = BehaviorSubject<String>(value: "")
		let pwd = BehaviorSubject<String>(value: "")
		let pwdTwo = BehaviorSubject<String>(value: "")
		
		let phoneValid = BehaviorSubject(value: false)
		let pwdLength = BehaviorSubject(value: false)
		let pwdFormat = BehaviorSubject(value: false)
		let pwdEqual = BehaviorSubject(value: false)

		let pwdValid = Observable.combineLatest(phoneValid, pwdLength, pwdFormat, pwdEqual)
			.map{(phoneValid:$0.0, pwdLength:$0.1,pwdFormat: $0.2, pwdEqual: $0.3)}
			.share(replay: 1)

		
		phoneTextField.rx.text
			.map({String(($0?.prefix(11))!)})
			.bind(to: phone)
			.disposed(by:disposeBag)

		phone.map({$0.isMatching("^1[3|4|5|7|8][0-9]{9}$")})
			.bind(to: phoneValid)
			.disposed(by: disposeBag)
		
		passwordTextField.rx.text
			.map({ String(($0?.prefix(16))!) })
			.bind(to: pwd)
			.disposed(by: disposeBag)
		
		phone.bind(to: phoneTextField.rx.text).disposed(by: disposeBag)
		pwd.bind(to: passwordTextField.rx.text).disposed(by: disposeBag)

		
		pwd.map({$0.count >= 8 && $0.count <= 16})
			.bind(to: pwdLength)
			.disposed(by: disposeBag)
		
		pwd.map({ $0.isMatching("^(?=.*\\d.*)(?=.*[a-z].*)(?=.*[A-Z].*)[0-9a-zA-Z]*$")})
			.bind(to: pwdFormat)
			.disposed(by: disposeBag)
		
		pwdLength.bind(to: passwordCountValidButton.rx.isSelected).disposed(by: disposeBag)
		pwdFormat.bind(to: passwordFormatValidButton.rx.isSelected).disposed(by: disposeBag)
				
		passwordTwiceTextField.rx.text
			.map({ String(($0?.prefix(11))!) })
			.bind(to: pwdTwo)
			.disposed(by: disposeBag)

		pwdTwo.bind(to: passwordTwiceTextField.rx.text).disposed(by: disposeBag)

		
		Observable.combineLatest(pwd, pwdTwo)
			.map({$0.0 == $0.1})
			.bind(to: pwdEqual)
			.disposed(by: disposeBag)
		
		pwdEqual.bind(to: passwordTipsLabel.rx.isHidden).disposed(by: disposeBag)
		
		bottomButton.rx.tap
			.withLatestFrom(pwdValid)
			.flatMapLatest { (tuple) -> Observable<Void> in

				guard tuple.phoneValid else {
					self.showHud(string: "请输入正确手机号")
					return .empty()
				}
				guard tuple.pwdLength else {
					self.showHud(string: "登录密码需 8~16 位")
					return .empty()
				}
				guard tuple.pwdFormat else {
					self.showHud(string: "必须包含大小写字母和数字")
					return .empty()
				}
				guard tuple.pwdEqual else {
					self.showHud(string: "两次密码输入不一致")
					return .empty()
				}
				self.showHud(string: "G o o d !!!")
				return .empty()
			}
			.subscribe(onNext: { _ in
				
			})
			.disposed(by: disposeBag)
	
	}
	
}
